using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Data.SqlClient;
using EMailProxyLib.DataAccess;

namespace EMailProxyLib.DataAccess
{
    public class DataAccess
    {
        /// <summary>
        /// 保存用户连接记录
        /// </summary>        
        public static void SaveConnectLog(Socket socket)
        {
            try
            {
                string ip = socket.RemoteEndPoint.ToString();

                string SQL = "INSERT INTO ConnectLog (ConnectIP,ConnectTime,RequestType) VALUES (@ConnectIP,@ConnectTime,@RequestType)";
                SqlParameter[] param = new SqlParameter[3];
                param[0] = new SqlParameter("@ConnectIP", ip);
                param[1] = new SqlParameter("@ConnectTime", DateTime.Now);
                param[2] = new SqlParameter("@RequestType", 1);

                DataProvider.Instance.ExecuteSQL(SQL, param);
            }
            catch { }
        }

        /// <summary>
        /// 保存用户连接记录
        /// </summary>        
        public static void SaveEmailLog(EmailBody email, bool success, string err)
        {
            try
            {
                string SQL = "INSERT INTO EmailHistory(SendTime,UserID,FromAddr,ToAddr,CC,BC,Attachments,FlagSuccess,ErrorText) VALUES (@SendTime,@UserID,@FromAddr,@ToAddr,@CC,@BC,@Attachments,@FlagSuccess,@ErrorText)";
                SqlParameter[] param = new SqlParameter[9];
                param[0] = new SqlParameter("@SendTime", DateTime.Now);
                param[1] = new SqlParameter("@UserID", email.UserID);
                param[2] = new SqlParameter("@FromAddr", email.From);
                param[3] = new SqlParameter("@ToAddr", email.To);
                param[4] = new SqlParameter("@CC", email.CC);
                param[5] = new SqlParameter("@BC", email.BC);
                param[6] = new SqlParameter("@Attachments", email.GetAttachmentFileNames());
                param[7] = new SqlParameter("@FlagSuccess", success ? "Y" : "N");
                param[8] = new SqlParameter("@ErrorText", err);
                DataProvider.Instance.ExecuteSQL(SQL, param);
            }
            catch { }
        }
    }
}
